package com.qf.smartdevice.events.listeners;

import com.qf.smartdevice.events.MyLoginEvent;
import com.qf.smartdevice.events.enums.LoginResultEnum;
import com.qf.smartdevice.mapper.SysLoginInforMapper;
import com.qf.smartdevice.mapper.SysUserOnlineMapper;
import com.qf.smartdevice.pojo.SysLogininfor;
import com.qf.smartdevice.pojo.SysUserOnline;
import com.qf.smartdevice.utils.RequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author 王博康
 * @version 1.0
 * @project devicemanagementsystem
 * @description: TODO
 * @date 2022/3/19 23:11 星期六
 */
@Component
//@EnableAsync//激活异步，只需要写一次，  之后的@Async才会有效，
public class LoginEventListener {
    
    private SysLoginInforMapper sysLoginInforMapper;

    @Autowired
    public void setSysLoginInforMapper(SysLoginInforMapper sysLoginInforMapper) {
        this.sysLoginInforMapper = sysLoginInforMapper;
    }


    private SysUserOnlineMapper sysUserOnlineMapper;

    @Autowired
    public void setSysUserOnlineMapper(SysUserOnlineMapper sysUserOnlineMapper) {
        this.sysUserOnlineMapper = sysUserOnlineMapper;
    }

    @EventListener
//    @Async//异步执行
    public void onEvent(MyLoginEvent loginEvent){

        //因为是通过threadlocal获取的数据,所以拿的时候不能放到子线程
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        //开启异步执行
        CompletableFuture.runAsync(()->{
            HttpServletRequest request = requestAttributes.getRequest();
            String SessionId = request.getSession().getId();//获取session的id

            //调用写好的工具类获取信息
            String ip = RequestUtil.getRemoteHost(request);//获取请求设备ip地址
            Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);
            String locationByIp = RequestUtil.getLocationByIp(ip);//根据ip地址获得对应地区

            //把数据放入对象中
            SysLogininfor logininfor = new SysLogininfor();
            logininfor.setLoginName(loginEvent.getUserName());
            logininfor.setBrowser(osAndBrowserInfo.get("browser"));
            logininfor.setOs(osAndBrowserInfo.get("os"));
            logininfor.setIpaddr(ip);
            logininfor.setLoginLocation(locationByIp);
            logininfor.setLoginTime(new Date());
            switch (loginEvent.getLoginResultEnum()){
                case SUCCESS:
                    logininfor.setMsg("登录成功");
                    logininfor.setStatus("1");
                    break;
                case FAIL:
                    logininfor.setMsg("登录失败");
                    logininfor.setStatus("0");
                    break;
            }

            //保存到数据库
            sysLoginInforMapper.addLoginLog(logininfor);

            //如果登录成功将在线用户信息添加到数据库
            if (loginEvent.getLoginResultEnum()== LoginResultEnum.SUCCESS){
                SysUserOnline userOnline = new SysUserOnline();
                BeanUtils.copyProperties(logininfor,userOnline );
                userOnline.setSessionId(SessionId);
                userOnline.setExpireTime(20L);
                userOnline.setStartTimestamp(new Date());

                //保存到数据库
                sysUserOnlineMapper.addUserOnline(userOnline);
            }

        });
    }
}
